package com.apkbus.mobile.utils;

import android.content.Context;

/**
 * Created by liyiheng on 16/9/19.
 */
public class DensityUtil {
    private DensityUtil(){}
    private static int width = 0;

    public static int getWidth(Context context) {
        if (width == 0) {
            width = context.getResources().getDisplayMetrics().widthPixels;
        }
        return width;
    }


    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
